import Vue from 'vue'
import './cube-ui'
import App from './App.vue'
import router from './router'
import store from './store'
import axios from 'axios'
import 'amfe-flexible'
import setaxios from './setaxios'
setaxios()

Vue.config.productionTip = false
Vue.prototype.$http = axios


//路由守卫
router.beforeEach((to, from, next) => {
  //无论刷新还是跳转路由，第一个进入的就是这个路由得前置勾子函数
  store.commit('setToken', localStorage.getItem('token'))
  if (to.meta.requireAuth) {
    if (store.state.token) {
      next()
    } else {
      next({
        path: '/login',
        query: { redirect: to.fullPath }//没有token的话就回首页并回到点击的页面
      })
    }
  } else {
    next()
  }
})
new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
